home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / prcgntn1.lha / Precognition / source / stringgadget.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  8KB  |  316 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "StringGadget.h"
  4. #include "StringGadgetClass.h"
  5. #include "minmax.h"
  6. #include "EmbossedGadgetClass.h"
  7. #include <proto/exec.h>
  8. #include <proto/intuition.h>
  9. #include <proto/graphics.h>
  10. #include "amigamem.h"
  11.  
  12.  
  13.  
  14. void StringGadget_CleanUp( StringGadget *self )
  15. {
  16.    struct StringInfo *stringinfo;
  17.  
  18.    stringinfo = (struct StringInfo *) self->g.SpecialInfo;
  19.  
  20.    if (stringinfo)
  21.    {
  22.       self->g.SpecialInfo = NULL;
  23.       Afree( stringinfo->Buffer );
  24.       Afree( stringinfo );
  25.    }
  26.    
  27.    Afree( self->BoxBorder );
  28.    self->BoxBorder = NULL;
  29. }
  30.  
  31.  
  32. #define BORDERWIDTH_X 4
  33. #define BORDERWIDTH_Y 2
  34.  
  35. Point StringGadget_SetLocation( StringGadget *self,
  36.                                  PIXELS        LeftEdge,
  37.                                  PIXELS        TopEdge )
  38. {
  39.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  40.    self->Location.x = LeftEdge;
  41.    self->Location.y = TopEdge;
  42.    self->g.LeftEdge = LeftEdge + BORDERWIDTH_X;
  43.    self->g.TopEdge  = TopEdge  + BORDERWIDTH_Y;
  44.    Permit();
  45. /*
  46.    printf("Location=(%d,%d) (%d,%d), LineWidth=(%d,%d)\n",
  47.       self->Location.x, self->Location.y,
  48.       self->g.LeftEdge, self->g.TopEdge,
  49.       LineWidth.x, LineWidth.y );
  50. */
  51.    return self->Location;
  52. }
  53.  
  54. #define FONT_HEIGHT 8
  55.  
  56. Point StringGadget_AskSize( StringGadget *self,
  57.                              PIXELS        Width,
  58.                              PIXELS        Height )
  59. {
  60.    Point size;
  61.  
  62.    size.x = MAX( Width,  6 + BORDERWIDTH_X*2 );
  63.    size.y = FONT_HEIGHT + BORDERWIDTH_Y*2;
  64.  
  65.    return size;
  66. }
  67.  
  68. Point StringGadget_SetSize( StringGadget *self,
  69.                             PIXELS        Width,
  70.                             PIXELS        Height )
  71. {
  72.  
  73.    pcg_3DBevel *bevel;
  74.    Point        size;
  75.  
  76.    bevel = (pcg_3DBevel *) self->BoxBorder;
  77.    size  = AskSize( self, Width, Height );
  78.  
  79.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  80.    self->Size       = size;
  81.    self->g.Width    = size.x - BORDERWIDTH_X*2;
  82.    self->g.Height   = size.y - BORDERWIDTH_Y*2;
  83.  
  84.    pcg_Init3DBevel( (pcg_3DBevel *) self->BoxBorder,
  85.                     -BORDERWIDTH_X, -BORDERWIDTH_Y, size.x, size.y, 0,
  86.                     self->Pens.BrightPen,  self->Pens.DarkPen, NULL );
  87.    Permit();
  88.    return size;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. USHORT StringGadget_ClaimEvent( StringGadget *self,
  95.                                 IntuiMessage *event )
  96. {
  97.    USHORT response = 0;
  98.  
  99.    switch (event->Class)
  100.    {
  101.       case REFRESHWINDOW:
  102.          response = RESPONDED;
  103.          break;
  104.  
  105.       case GADGETDOWN:
  106.          if (event->IAddress == (void*) &self->g)
  107.          {
  108.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  109.          }
  110.          break;
  111.  
  112.       case GADGETUP:
  113.          if (event->IAddress == (void*) &self->g)
  114.          {
  115.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE | DEACTIVATED;
  116.          }
  117.          break;
  118.    }
  119.  
  120.    return response;
  121. }
  122.  
  123.  
  124. void StringGadget_Refresh( StringGadget *self )
  125. {
  126.    pcgWindow *window;
  127.  
  128.    if (window = InteractorWindow(self))
  129.    {
  130.       EmbossedGadget_Refresh( self );
  131.  
  132.       if (self->LabelText.IText)
  133.          PrintIText( RPort(window), &self->LabelText,
  134.                      self->Location.x, self->Location.y );
  135.    }
  136. }
  137.  
  138. void StringGadget_Render( StringGadget *self, RastPort *RPort )
  139. {
  140.  
  141.    EmbossedGadget_Render( self, RPort );
  142.  
  143.    if (self->LabelText.IText)
  144.       PrintIText( RPort, &self->LabelText,
  145.                   self->Location.x, self->Location.y );
  146. }
  147.  
  148.  
  149.  
  150.  
  151. BOOL StringGadget_Activate( StringGadget *self )
  152. {
  153.    pcgWindow *pw;
  154.    Window *w;
  155.  
  156.    if (isEnabled(self))
  157.       if (pw = InteractorWindow(self))
  158.          if (w = iWindow(pw))
  159.          {
  160.             ActivateGadget( FirstGadget(self), w, NULL );
  161.             return TRUE;
  162.          }
  163.    return FALSE;
  164. }
  165.  
  166. char *StringGadget_Value( StringGadget *self )
  167. {
  168.    struct StringInfo *stringinfo;
  169.  
  170.    stringinfo = (StringInfo *) self->g.SpecialInfo;
  171.  
  172.    if (stringinfo->NumChars > 0)
  173.       return (char*) stringinfo->Buffer;
  174.    else return NULL;
  175. }
  176.  
  177. char *StringGadget_SetValue( StringGadget *self, char *selection )
  178. {
  179.    struct StringInfo *stringinfo;
  180.  
  181.    stringinfo = (StringInfo *) self->g.SpecialInfo;
  182.    Forbid();
  183.  
  184.    if (selection)
  185.       strncpy( stringinfo->Buffer, selection, stringinfo->MaxChars );
  186.    else
  187.       stringinfo->Buffer[0] = '\0';
  188.  
  189.    Permit();
  190.  
  191.    return selection;
  192. }
  193.  
  194. #ifdef BUILDER
  195. #include "BuilderMethods.h"
  196. #include "GraphicObject_Builder.h"
  197. #include "StringGadget_coder.h"
  198. #include "StringGadget_Builder.h"
  199.  
  200. StringGadget *StringGadget_New( StringGadget *self )
  201. {
  202.    StringGadget *new_gadget;
  203.    struct StringInfo *stringinfo;
  204.  
  205.    if (new_gadget = (StringGadget *) Amalloc( sizeof(StringGadget) ))
  206.    {
  207.       stringinfo = (struct StringInfo *) self->g.SpecialInfo;
  208.  
  209.        StringGadget_Init( new_gadget,
  210.           self->Location.x, self->Location.y,
  211.           self->Size.x, stringinfo->MaxChars-1,
  212.           self->Pens,   Title(self) );
  213.        new_gadget->g.Flags      = self->g.Flags;
  214.        GiveItAName( new_gadget );
  215.        SetStringValue( new_gadget, StringValue(self) );
  216.    }
  217.    return new_gadget;
  218. }
  219.  
  220.  
  221. struct BuilderMethods StringGadget_bm;
  222.  
  223. #endif
  224.  
  225.  
  226. BOOL StringGadget_elaborated = FALSE;
  227.  
  228. struct ValuatorClass StringGadget_Class;
  229.  
  230. void StringGadgetClass_Init( struct ValuatorClass *class )
  231. {
  232.    ValuatorClass_Init( class );
  233.    EmbossedGadgetClass_Init( class );
  234.    class->isa         = ValuatorClass();
  235.    class->ClassName   = "StringGadget";
  236.    class->CleanUp     = StringGadget_CleanUp;
  237.    class->SetLocation = StringGadget_SetLocation;
  238.    class->SetSize     = StringGadget_SetSize;
  239.    class->AskSize     = StringGadget_AskSize;
  240.    class->SizeFlags   = GraphicObject_SizeFlagsX; /* X axis only */
  241.    class->Render      = StringGadget_Render;
  242.    class->ClaimEvent  = StringGadget_ClaimEvent;
  243.    class->Respond     = StringGadget_ClaimEvent;
  244.    class->Refresh     = StringGadget_Refresh;
  245.    class->Activate    = StringGadget_Activate;
  246.    class->isActive    = NULL;
  247.    class->Value       = StringGadget_Value;
  248.    class->SetValue    = StringGadget_SetValue;
  249.  
  250. #ifdef BUILDER
  251.    class->BuilderMethods = &StringGadget_bm;
  252.    go_InitBuilderMethods( &StringGadget_bm );
  253.    StringGadget_bm.New       = StringGadget_New;
  254.    StringGadget_bm.WriteCode = StringGadget_WriteCode;
  255.    StringGadget_bm.PropEdit  = StringGadget_PropEdit;
  256. #endif
  257. }
  258.  
  259.  
  260. struct ValuatorClass *StringGadgetClass( void )
  261. {
  262.    if (! StringGadget_elaborated)
  263.    {
  264.       StringGadgetClass_Init( &StringGadget_Class );
  265.       StringGadget_elaborated = TRUE;
  266.    }
  267.  
  268.    return &StringGadget_Class;
  269. }
  270.  
  271.  
  272.  
  273. void StringGadget_Init( StringGadget *self,
  274.                         PIXELS        LeftEdge,
  275.                         PIXELS        TopEdge,
  276.                         PIXELS        Width,
  277.                         USHORT        nChars,
  278.                         pcg_3DPens    Pens,
  279.                         char         *label )
  280. {
  281.    struct StringInfo *stringinfo;
  282.    AlignInfo ainfo;
  283.  
  284.  
  285.    EmbossedGadget_Init( self, LeftEdge, TopEdge, Width, 0,
  286.          GADGHCOMP, RELVERIFY, STRGADGET, Pens, NULL );
  287.  
  288.    self->isa             = StringGadgetClass();
  289.    Afree( self->BoxBorder );
  290.    self->BoxBorder       = (pcg_3DBox *) Acalloc(1, sizeof(pcg_3DBevel));
  291.  
  292.    SetLocation( self, LeftEdge, TopEdge );
  293.    SetSize( self, Width, 0 );
  294.  
  295.    ainfo = TextAlignment(self);
  296.    SetTextAlignment( self, tx_OUTSIDE | tx_LEFT | tx_YCENTER,
  297.       ainfo.Xpad, ainfo.Ypad );
  298.  
  299.  
  300.    stringinfo = (struct StringInfo *) Acalloc(1, sizeof(struct StringInfo));
  301.    stringinfo->BufferPos  = 0;
  302.    stringinfo->DispPos    = 0;
  303.    stringinfo->MaxChars   = nChars+1;
  304.    stringinfo->Buffer     = Acalloc( 1, nChars+1 );
  305.    stringinfo->UndoBuffer = NULL;
  306.  
  307.    self->g.GadgetRender  = (APTR) &self->BoxBorder->TopLeft;
  308.    self->g.SelectRender  = NULL;
  309.    self->g.GadgetText    = NULL;
  310.    self->g.MutualExclude = 0L;
  311.    self->g.SpecialInfo   = (APTR) stringinfo;
  312.  
  313.    SetTitle( self, label );
  314. }
  315.  
  316.